from .abs_quantifier import BaseQuantifier
from ..data_structures.base_structures import DemAttribute, Map, HexCell
from ..config import *
from .shp_quantifier_components import *
from .tif_quantifier_components import *
import geopandas as gpd
from shapely.geometry import Polygon,MultiPolygon
from tqdm import tqdm
import h3
import rasterio

class TifQuantifier(BaseQuantifier):

    # tif_quantifier_components_hex = [
    #     # TifQuantifierComponents.QuantifyHexcellsWithCurvature,
    #     # TifQuantifierComponents.QuantifyHexcellsWithCv,
    #     # TifQuantifierComponents.QuantifyHexcellsWithElevation,
    #     # TifQuantifierComponents.QuantifyHexcellsWithExposure,
    #     # TifQuantifierComponents.QuantifyHexcellsWithRelief,
    #     # TifQuantifierComponents.QuantifyHexcellsWithRoughness,
    #     # TifQuantifierComponents.QuantifyHexcellsWithSlope,
    # ]
    # tif_quantifier_components_quad = []
    #
    # def add_component(component:str, grid_type='hex'):
    #     if grid_type == 'hex':
    #         TifQuantifier.tif_quantifier_components_hex.append(component)
    #     elif grid_type == 'quad':
    #         TifQuantifier.tif_quantifier_components_quad.append(component)
    #     else:
    #         raise ValueError(f"不支持的网格类型，应为 'hex' 或 'quad'")

    tif_quantifier_components = [
        # TifQuantifierComponents.QuantifyHexcellsWithCurvature,
        # TifQuantifierComponents.QuantifyHexcellsWithCv,
        # TifQuantifierComponents.QuantifyHexcellsWithElevation,
        # TifQuantifierComponents.QuantifyHexcellsWithExposure,
        # TifQuantifierComponents.QuantifyHexcellsWithRelief,
        # TifQuantifierComponents.QuantifyHexcellsWithRoughness,
        # TifQuantifierComponents.QuantifyHexcellsWithSlope,
    ]

    def add_component(component:str):
        TifQuantifier.tif_quantifier_components.append(component)


    def quantify_hexcells(path:str, map:Map) -> Map:
        """量化六角格网的tif文件"""
        # 读取DEM数据(wgs84坐标系)
        with rasterio.open(path) as src:
            data = src.read(1)          # 读取第一波段数据
            transform = src.transform   # 获取仿射变换
            nodata_value = src.nodata   # 获取缺省值

        # 反射调用方法
        for component in TifQuantifier.tif_quantifier_components:
            method_name = component
            method = getattr(TifQuantifierComponents, method_name, None)
            if method is None:
                raise AttributeError(f"未找到组件方法: {method_name}")
            try:
                method(data, transform, nodata_value, map)
            except Exception as e:
                print(f"调用组件方法 {method_name} 时出错: {e}")

    def quantify_quadcells(path: str, map: Map) -> Map:
        """量化四角格网tif文件"""
        # 读取DEM数据(wgs84坐标系)
        with rasterio.open(path) as src:
            data = src.read(1)  # 读取第一波段数据
            transform = src.transform  # 获取仿射变换
            nodata_value = src.nodata  # 获取缺省值

        # 反射调用方法
        for component in TifQuantifier.tif_quantifier_components:
            method_name = component
            method = getattr(TifQuantifierComponents, method_name, None)
            if method is None:
                raise AttributeError(f"未找到组件方法: {method_name}")
            try:
                method(data, transform, nodata_value, map)
            except Exception as e:
                print(f"调用组件方法 {method_name} 时出错: {e}")